home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cppcom.zip / UART.HPP < prev   
C/C++ Source or Header  |  1991-04-14  |  6KB  |  234 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and freely used for any purpose.
  7.  
  8. Enhancements: 1991 by David Orme
  9.     *  General cleanup.
  10.             - I/O now takes advantage of C++ overloading.
  11.             - Serial port I/O functionality now only in Serial class.
  12.             - Modem functionality now only in Modem class.
  13.     *  Possible to easily implement file Xfr prots now.
  14.     *  CCITT CRC-16 class added                            -- 2-20-1991
  15.     *  BIOS Timer class added                                -- 2-22-1991
  16.     *  Optional timeout on all input routines added    -- 2-25-1991
  17.  
  18. ***************************************************************************/
  19.  
  20. // file uart.hpp, class declaration for the uart class.
  21. // see your modem manual (I used MultiTech's) or the IBM Technical
  22. // reference manual for more information on the 8250 UART used in the PC.
  23.  
  24.  
  25. #ifndef UART_HPP
  26. #define UART_HPP 1
  27.  
  28. #include <dos.h>
  29. #include <bool.h>
  30.  
  31.  
  32. #ifdef __ZTC__
  33.  
  34. #include <int.h>
  35.  
  36. #ifndef inportb
  37. #define inportb inp
  38. #define outportb outp
  39. #define inportw inpw
  40. #define outportw outpw
  41. #define disable int_off
  42. #define enable int_on
  43. #endif
  44.  
  45. #endif
  46.  
  47.  
  48. // N. B.: Be VERY careful when using some PC hardware with COM1
  49. // or COM2 at the same time this class or its derivative classes are set
  50. // to use COM3 or COM4! This includes hardware incompatibilities with 
  51. // certain serial rodents.  On the IBM PC, the interrupts for 
  52. // COM1 & COM4 and for COM2 & COM3 are the same and tend to collide.
  53.  
  54. const int NUM_PORTS = 4;
  55. // COM1 thru COM4, but see above.
  56.  
  57.  
  58.  
  59. // The 8250 UART uses some truly weird methods of signaling interrupt
  60. // type.  The enum type below tries to make this into a hopefully more
  61. // comprehensible enum set.
  62.  
  63. enum com_interrupt_t { 
  64.         NONE_PENDING,
  65.         RING,
  66.         CARRIER, 
  67.         NO_CARRIER,
  68.         TRANSMIT_READY,
  69.         TRANSMIT_FALSE_ALARM,
  70.         RECEIVE_READY,
  71.         OVERRUN_ERROR,
  72.         PARITY_ERROR,
  73.         FRAMING_ERROR,
  74.         BREAK_RECEIVED,
  75.         UNKNOWN_ERROR
  76.     };                           
  77.  
  78.  
  79. // Parity is a concept which is hard to represent as a set
  80. // of integers.  The enum set below takes its values for the
  81. // convenience of setting parity on the 8250 UART line control
  82. // register (LCR).
  83.  
  84. enum parity_t
  85. {
  86.     NOPAR =  0x00,                // No parity 
  87.     ODDPAR = 0x08,                // Odd parity 
  88.     EVNPAR = 0x18,                // Even parity 
  89.     STKPAR = 0x28,                // Stick parity 
  90.     ZROPAR = 0x38                 // Zero parity; not well documented
  91. };
  92.  
  93. // This typedef helps the compiler sort out parameter types.
  94. #ifdef __TURBOC__
  95. typedef void interrupt (* DRIVER)(...);
  96. #else ifdef __ZTC__
  97. extern "C" {
  98. typedef int (*DRIVER)(INT_DATA *);
  99. };
  100. #endif
  101.  
  102.  
  103. // Here is the uart class itself.  It uses a method of calling derived
  104. // class methods for base class functions which is occasionally buggy
  105. // on TC++ 1.0 when using inline functions.  Take care, therefore, in
  106. // any attempt to inline the member functions below, since they call
  107. // virtual functions.
  108.  
  109. class uart
  110. {
  111. protected:
  112.     static const int portvector_num[NUM_PORTS];
  113.     static const char intmaskbit[NUM_PORTS];
  114.     static char old_intmask[NUM_PORTS];
  115.     static char old_MCR[NUM_PORTS];
  116.     static char old_IER[NUM_PORTS];
  117. #ifdef __TURBOC__
  118.     static DRIVER old_driver[NUM_PORTS];
  119. #else ifdef __ZTC__
  120.     static void far * old_driver[NUM_PORTS];
  121. #endif
  122.  
  123. public:
  124.     uart();
  125.     virtual ~uart();
  126.     // note that there is a static virtual destructor "heap is corrupted" 
  127.     // bug in certain ZTC versions of this code which may bite here.
  128.  
  129.     int RegisterDriver(int portnum, DRIVER driv);
  130.     int RestoreDriver(int portnum);
  131.     
  132.     // These are constants of the PC 8250 UART.
  133.     // From the MultiTech Systems Owners' Manual 
  134.     // and the IBM Technical Reference Manual.
  135.     // These are abstract and must be defined by a derived class.
  136.  
  137.     virtual unsigned int LCR() = 0;
  138.     virtual unsigned int DLL() = 0;
  139.     virtual unsigned int DLM() = 0;
  140.     virtual unsigned int LSR() = 0;
  141.     virtual unsigned int MCR() = 0;
  142.     virtual unsigned int MSR() = 0;
  143.     virtual unsigned int THR() = 0;
  144.     virtual unsigned int RBR() = 0;
  145.     virtual unsigned int IER() = 0;
  146.     virtual unsigned int IIR() = 0;
  147.  
  148.     char GetLCR();
  149.     char GetDLL();
  150.     char GetDLM();
  151.     char GetLSR();
  152.     char GetMCR();
  153.     char GetMSR();
  154.     char GetRBR();
  155.     char GetIER();
  156.     char GetIIR();
  157.  
  158.     boolean GetLSR_THRE();
  159.  
  160.     void SetLCR(char byte);
  161.     void SetDLL(char byte);
  162.     void SetDLM(char byte);
  163.     void SetMCR(char byte);
  164.     void SetMSR(char byte);
  165.     void SetLSR(char byte);
  166.     void SetTHR(char byte);
  167.     void SetIER(char byte);
  168.  
  169.     void SetIER_Recieve(boolean bit);
  170.     void SetIER_Transmit(boolean bit);
  171.     void SetIER_Line(boolean bit);
  172.     void SetIER_Modem(boolean bit);
  173.  
  174.     void SetLCR_DLAB(boolean bit);
  175.  
  176.     void SetLSR_DR(boolean bit);
  177.     
  178.     void SetBaudRate(int speed);        
  179.     int GetBaudRate();
  180.     void SetSpeed(int speed) { SetBaudRate(speed); }
  181.     int GetSpeed() { return GetBaudRate(); }
  182.  
  183.     void SetParity(parity_t p);
  184.     parity_t GetParity();
  185.  
  186.     void SetWordLength(int len);
  187.     int GetWordLength();
  188.     void SetDataBits(int num) { SetWordLength(num); }
  189.     int GetDataBits() { return GetWordLength(); }
  190.  
  191.     void SetStopBits(int num);
  192.     int GetStopBits();
  193.  
  194.  
  195.     void SetBreak();
  196.     void StopBreak();
  197.     void Break(int msec = 500);
  198.     void Pause(int msec = 500);
  199.  
  200.     void SetCTS(boolean bit);
  201.     void SetDSR(boolean bit);
  202.  
  203.     boolean CarrierPresent();
  204.  
  205.     boolean GetDTR();
  206.     void SetDTR(boolean bit);
  207.  
  208.     com_interrupt_t GetIntrType();
  209.  
  210.     virtual void SetDoIfNoCarrier(void (*f)()) = 0;
  211.     virtual void SetDoOnRing(void (*f)()) = 0;
  212.  
  213.     virtual void PurgeInput() = 0;
  214.     virtual void FlushOutput() = 0;
  215.     virtual void PurgeOutput() = 0;
  216.     virtual boolean InputEmpty() = 0;
  217.     virtual boolean OutputEmpty() = 0;
  218.     virtual boolean OutputReady() = 0;
  219.  
  220.     // Note: the functions below do NO error checking.
  221.     // You must do your error checking in your driver routine.
  222.  
  223.     virtual int GetChar();
  224.     virtual void SendChar(char ch);
  225.     virtual void TransmitChar(char ch);
  226.     virtual int ReceiveChar();
  227. };
  228.  
  229.  
  230. #endif
  231.  
  232. // end of file uart.hpp
  233.  
  234.